package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.dao.PoemDao;
import com.coding.dao.PoemPkDao;
import com.coding.dao.UserDao;
import com.coding.pojo.PoemPkUser;
import com.coding.pojo.User;
import com.coding.service.PoempkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @auther wangchenghao
 * @date 2020/7/22
 **/
@Service
public class PoempkImpl implements PoempkService {
    //定义保存所有Socket的Arraylist
    public static ArrayList<Socket> socketArrayList = new ArrayList<Socket>();
    public static ArrayList<Socket> pkList = new ArrayList<Socket>();
    public static volatile boolean connectFlag;
    public static volatile boolean pkFlag;
    ServerSocket serverSocket;
    @Autowired
    PoemDao poemDao;
    @Autowired
    PoemPkDao poemPkDao;
    @Autowired
    UserDao userDao;

    @Override
    public void start(int user_id, BaseModel baseModel) throws Exception {
//        if(socketArrayList.size()==1&&serverSocket !=null){
//            baseModel.setMessage("匹配成功,当前匹配成功人数：" + socketArrayList.size());
//            baseModel.setResultCode(1);
//            baseModel.setData(socketArrayList.size());
//            return;
//        }
//
//        if (socketArrayList.size() > 0) {
//            socketArrayList.clear();
//        }
//        if (serverSocket != null) {
//            serverSocket.close();
//        }
        try {
            if (serverSocket == null || serverSocket.isClosed()) {
                serverSocket = new ServerSocket(39000);
                serverSocket.setSoTimeout(80000);
                connectFlag = true;
                pkFlag = true;
                Timer nTimer = new Timer();
                nTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        System.out.println("----设定要指定任务-----");
                        closeServerSocket();
                    }
                }, 100000);
            }

            /**
             * 设定2000毫秒后执行
             */

            Socket socket = new Socket();


            new Thread(new TestThreadImpl(socket, serverSocket, poemDao, poemPkDao, user_id)).start();

//

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(socketArrayList.size());
        baseModel.setMessage("当前匹配成功人数：" + socketArrayList.size());
        baseModel.setResultCode(1);
        baseModel.setData(socketArrayList.size());

    }

    @Override
    public void getstatus(BaseModel baseModel) throws Exception {
        if (socketArrayList.size() > 0) {
            for (Socket socket : socketArrayList
            ) {
                System.out.println(socket.getInetAddress() + ":" + socket.getChannel());
            }
        }

        baseModel.setData(socketArrayList.size());
        baseModel.setResultCode(1);
        baseModel.setMessage("目前线程个数");
    }

    @Override
    public void getPoekInfo(BaseModel baseModel) throws Exception {
        PoemPkUser poemPkUser = poemPkDao.selectNewPoemPK();
        if (poemPkUser != null) {
            if (poemPkUser.getUser_one_id() > 0) {
                User userOne = userDao.selectById(poemPkUser.getUser_one_id());
                if (userOne != null) {
                    poemPkUser.setUserOne(userOne);
                }
            }
            if (poemPkUser.getUser_two_id() > 0) {
                User userTwo = userDao.selectById(poemPkUser.getUser_two_id());
                if (userTwo != null) {
                    poemPkUser.setUserTwo(userTwo);
                }
            }
            baseModel.setSucceed("查询到飞花令双方信息", poemPkUser);
        } else {
            baseModel.setFailure("未查询到飞花令双方信息");
        }
    }

    //清理线程并初始化
    public void closeServerSocket() {
        try {
            serverSocket.close();
            socketArrayList.clear();
            connectFlag = true;
            pkFlag = true;
            System.out.println("线程已关闭");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
